package com.tensquare.user.service;

import com.tensquare.entity.IdWorker;
import com.tensquare.user.dao.AdminDao;
import com.tensquare.user.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author zzb
 * @create 2021/2/23
 */
@Service
public class AdminService {

    @Autowired
    private AdminDao adminDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    //添加管理员
    public void add(Admin admin) {
        //设置管理员ID
        long id = idWorker.nextId();
        admin.setId(id+"");
        //将密码进行加密
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminDao.save(admin);

    }

    //管理员登录
    public Admin login(Admin admin) {
        //根据登录名查询用户
        Admin loginAdmin = adminDao.findByLoginname(admin.getLoginname());
        //进行比对判断是否幼稚
        if (loginAdmin !=null && passwordEncoder.matches(admin.getPassword(),loginAdmin.getPassword())){
            return loginAdmin;
        }
        return null;
    }

    //根据id删除
    public void deleteById(String adminId) {
        adminDao.deleteById(adminId);
    }
}
